package com.feng.chat.controller;

import com.feng.chat.dto.Result;
import com.feng.chat.entity.TalkRecords;
import com.feng.chat.service.*;
import com.feng.chat.service.impl.TalkRecordsServiceImpl;
import com.feng.chat.utils.GetUserUtil;
import com.feng.chat.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/talk")
public class TalkController {
    @Autowired
    private ITalkSessionService talkSessionService;

    @Autowired
    private ITalkRecordsService talkRecordsService;

    @Autowired
    private ITalkRecordsFileService talkRecordsFileService;

    @Autowired
    private IEmoticonService emoticonService;

    @Autowired
    private ITalkRecordsCodeService talkRecordsCodeService;

    @Autowired
    private ITalkRecordsVoteService talkRecordsVoteService;

    /**
     * 获取聊天列表信息
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result talkList(HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkSessionService.TalkList(userId);
        return result;
    }

    /**
     * 创建会话
     * @param talkCreateVO
     * @param request
     * @return
     */
    @PostMapping("/create")
    public Result create(@RequestBody TalkCreateVO talkCreateVO,HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkSessionService.create(userId, talkCreateVO);
        return result;
    }

    /**
     * 移除会话
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String,Integer> map){
        Result result = talkSessionService.delete(map.get("list_id"));
        return result;
    }

    /**
     * 清除未读信息
     * @param map
     * @param request
     * @return
     */
    @PostMapping("/unread/clear")
    public Result unreadClear(@RequestBody Map<String,Integer> map,HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Integer receiver_id = map.get("receiver_id");
        Integer talk_type = map.get("talk_type");
        Result result = talkRecordsService.unreadClear(userId, receiver_id, talk_type);
        return result;
    }

    /**
     * 获取聊天信息
     * @param receiver_id
     * @param talk_type
     * @param limit
     * @param request
     * @return
     */
    @GetMapping("/records")
    public Result records(Integer receiver_id,Integer record_id, Integer talk_type,Integer limit,HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsService.records(receiver_id,record_id, talk_type, limit, userId);
        return result;
    }

    /**
     * 发送文本消息
     * @param textVO
     * @param request
     * @return
     */
    @PostMapping("/message/text")
    public Result text(@RequestBody TextVO textVO,HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsService.text(textVO, userId);
        return result;
    }


    /**
     * 消息撤回
     * @param map
     * @return
     */
    @PostMapping("/message/revoke")
    public Result revoke(@RequestBody Map<String,Integer> map){
        Integer record_id = map.get("record_id");
        Result result = talkRecordsService.revoke(record_id);
        return result;
    }

    /**
     * 消息删除
     * @param map
     * @return
     */
    @PostMapping("/message/delete")
    public Result delete(@RequestBody Map<String,Integer> map,HttpServletRequest request){
        Integer receiver_id = map.get("receiver_id");
        Integer record_id = map.get("record_id");
        Integer talk_type = map.get("talk_type");
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsService.delete(receiver_id, record_id, talk_type, userId);
        return result;
    }

    /**
     * 消息转发
     * @param messageForwardVO
     * @param request
     * @return
     */
    @PostMapping("/message/forward")
    public Result forward(@RequestBody MessageForwardVO messageForwardVO, HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsService.forward(messageForwardVO, userId);
        return result;
    }

    /**
     * 获取会话消息详情
     * @param record_id
     * @param request
     * @return
     */
    @GetMapping("/records/forward")
    public Result recordsForward(Integer record_id,HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsService.recordsForward(record_id, userId);
        return result;
    }

    /**
     * 发送表情消息
     * @param request
     * @return
     */
    @PostMapping("/message/emoticon")
    public Result messageEmoticon(HttpServletRequest request, @RequestBody EmoticonVO emoticonVO){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsFileService.messageEmoticon(userId, emoticonVO);
        return result;
    }

    /**
     * 收藏表情包
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/message/collect")
    public Result messageCollect(HttpServletRequest request,@RequestBody Map<String,Integer> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = emoticonService.messageCollect(userId, map.get("record_id"));
        return result;
    }

    /**
     * 获取聊天历史记录
     * @param request
     * @param talk_type
     * @param receiver_id
     * @param record_id
     * @param msg_type
     * @param limit
     * @return
     */
    @GetMapping("/records/history")
    public Result recordsHistory(HttpServletRequest request,Integer talk_type,Integer receiver_id,Integer record_id,Integer msg_type,Integer limit){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsService.recordsHistory(userId, talk_type, receiver_id, record_id, msg_type, limit);
        return result;
    }

    /**
     * 设置群免打扰状态
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/disturb")
    public Result disturb(HttpServletRequest request,@RequestBody Map<String,Integer> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkSessionService.disturb(userId, map.get("is_disturb"), map.get("receiver_id"), map.get("talk_type"));
        return result;
    }

    /**
     * 会话列表置顶
     * @param map
     * @return
     */
    @PostMapping("/topping")
    public Result topping(@RequestBody Map<String,Integer> map){
        Result result = talkSessionService.topping(map.get("list_id"), map.get("type"));
        return result;
    }

    /**
     * 发送代码消息
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/message/code")
    public Result messageCode(HttpServletRequest request,@RequestBody Map<String,String> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsCodeService.messageCode(userId, map.get("code"), map.get("lang"), Integer.parseInt(map.get("receiver_id")), Integer.parseInt(map.get("talk_type")));
        return result;
    }

    /**
     * 发送图片消息
     * @param request
     * @param imageVO
     * @return
     */
    @PostMapping("/message/image")
    public Result messageImage(HttpServletRequest request, ImageVO imageVO){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsFileService.messageImage(userId, imageVO.getTalk_type(), imageVO.getReceiver_id(), imageVO.getImage());
        return result;
    }

    /**
     * 发送投票信息
     * @param request
     * @param messageVoteVO
     * @return
     */
    @PostMapping("/message/vote")
    public Result messageVote(HttpServletRequest request,@RequestBody MessageVoteVO messageVoteVO){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsVoteService.messageVote(userId, messageVoteVO.getMode(), messageVoteVO.getOptions(), messageVoteVO.getReceiver_id(), messageVoteVO.getTitle());
        return result;
    }

    /**
     * 用户投票
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/message/vote/handle")
    public Result voteHandle(HttpServletRequest request,@RequestBody Map<String,String> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsVoteService.voteHandle(userId, map.get("options"), Integer.parseInt(map.get("record_id")));
        return result;
    }

    /**
     * 发送文件信息
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/message/file")
    public Result messageFile(HttpServletRequest request,@RequestBody Map<String,String> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = talkRecordsFileService.messageFile(userId, Integer.parseInt(map.get("receiver_id")), Integer.parseInt(map.get("talk_type")), map.get("upload_id"));
        return result;
    }

    /**
     * 文件下载
     * @param response
     * @param cr_id
     */
    @GetMapping("/records/file/download")
    public void fileDownload(HttpServletResponse response,Integer cr_id){
       talkRecordsFileService.fileDownload(response,cr_id);
    }
}
